from lxml import etree
from pyquery import PyQuery as pq
import requests

headers={
    'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
    'referer':'https://movie.douban.com/'
}

def getInfo(listInfo):
    HTML=requests.get('https://movie.douban.com/cinema/later/chengdu/')
    HTML.encoding=HTML.apparent_encoding
    text=HTML.text
    html=etree.HTML(text)
    divs=html.xpath('//div[@class="item mod odd"]')
    movie_template={
        'img':"",
        'title':"",
        'date':"",
        'type':"",
        'addr':"",
        'want':""
    }
    for div in divs:
        movie=movie_template.copy()
        movie['img']=div.xpath('.//img/@src')[0].encode('utf-8').decode('utf-8')
        movie['title']=div.xpath('./div[@class="intro"]/h3/a/text()')[0].encode('utf-8').decode('utf-8')
        lis=div.xpath('.//li')
        movie['date']=lis[0].xpath('./text()')[0].encode('utf-8').decode('utf-8')
        movie['type']=lis[1].xpath('./text()')[0].encode('utf-8').decode('utf-8')
        movie['addr']=lis[2].xpath('./text()')[0].encode('utf-8').decode('utf-8')
        movie['want']=lis[3].xpath('.//text()')[0].encode('utf-8').decode('utf-8')
        listInfo.append(movie)

def print_list(listInfo):
    for info in listInfo:
        print("{")
        for key,value in info.items():
            print("\"",key,'\":\"',value,"\"")
        print("}")

def put_in_file(listInfo):
    f=open('collection/douban.txt','w')
    f.close()
    with open('collection/douban.txt','a',encoding='utf-8') as f:
        f.write("{")
        for info in listInfo:
            f.write("{\n")
            for key,value in info.items():
                f.write("\""+key+'\":\"'+value.encode('gbk').decode('gbk')+"\"\n")
            f.write("}\n")
        f.write("}")

if __name__=='__main__':
    listInfo=[]
    getInfo(listInfo)
    put_in_file(listInfo)
